<?php
/**
 * Created by PhpStorm.
 * User: 12133
 * Date: 2025/6/20
 * Time: 9:35
 */

namespace app\model;


use think\facade\Db;
use think\Model;

class Base extends Model
{
    protected $append = [
        'image',
        'images'
    ];

    public function getImageAttr($value,$data){
        if (empty($data['image_id']))return '';
        $protocol = (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off') ? 'https://' : 'http://';
        $web = $protocol.$_SERVER['SERVER_NAME'];
        if ($_SERVER['SERVER_PORT'] != '80') $web .= ':'.$_SERVER['SERVER_PORT'];
        $find = Image::find($data['image_id']);
        if (!empty($find->id))
            return $web.Image::find($data['image_id'])->src;
        else
            return $web.'/zz.gif';
    }

    public function getImagesAttr($value,$data){
        if (empty($data['images_id']))return [];
        if (is_null($data['images_id']))return [];
        $images = json_decode($data['images_id']);
        foreach ($images AS $key => $item) $ids[] = $item;
        if (empty($ids))return [];
        $list = Image::where('id','IN',$ids)->select();
        foreach ($list AS $key => $value) {
            if ($value->status == 0)$value->save(['status' => 1]);
            $array[] = array_merge($value->toArray(), ['image' => getweb() . $value->src]);
        }
        return $array ?? [];
    }
}